package cn.kgc.vue.handler;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/8/10
 * @Description: 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 用户名不存在
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.UNKNOWN_ACCOUNT);
        return responseResult;
    }

    // 密码错误   IncorrectCredentialsException
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.INCORRECT_CREDENTIALS);
        return responseResult;
    }

    //账户被禁用  LockedAccountException
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.LOCKED_ACCOUNT);
        return responseResult;
    }

    // token过期
    @ExceptionHandler(TokenExpiredException.class)
    public ResponseResult tokenExpiredExceptionHandler(TokenExpiredException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_EXPIRED);
        return responseResult;
    }

    // token异常
    @ExceptionHandler({SignatureVerificationException.class, JWTDecodeException.class})
    public ResponseResult tokenExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_ERROR);
        return responseResult;
    }

    // 认证异常  AuthenticationException
    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult authenticationExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.AUTHENTICATION_ERROR);
        return responseResult;
    }

    //权限异常   AuthorizationException
    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authenticationExceptionHandler(AuthorizationException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.AUTHORIZATION_ERROR);
        return responseResult;
    }

    // 其它异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.FAIL);
        return responseResult;
    }
    
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMsg("参数校验异常");
        return responseResult;
    }
}
